package com.flyee.utils
{
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Dictionary;
	import flash.utils.Timer;

	public class EventManager 
	{
		public static var eventSprite:Sprite = new Sprite()
		public static var downLoadSpeed:uint = 33;
		public static var myDictionary:Dictionary = new Dictionary();
		public static var registLoaderArray:Array = new Array();
		public static var LOWER_SPEED:uint = 40;
		public function EventManager()
		{
			
		}
		public static function initEventManager(tagSprite:Sprite):void
		{
			EventManager.eventSprite = tagSprite
		}
		public static function registListener(tagType:String,tagFun:Function,tagObject:Object = null,useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			EventManager.eventSprite.addEventListener(tagType,tagFun,useCapture,priority,useWeakReference);
			//EventUtils.addListener(eventSprite,tagType,tagFun,tagObject,null,useCapture,priority,useWeakReference)
		}
		public static function dispatherTagEvent(event:Event):void
		{
			EventManager.eventSprite.dispatchEvent(event);
		}
		public static function removeTagListener(tagType:String,tagFun:Function,useCapture:Boolean=false):void
		{
			EventManager.eventSprite.removeEventListener(tagType,tagFun,useCapture);
		}
		public static function registLoadEvent(tagType:String,tagFun:Function,tagObject:Object,useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
		{
			EventManager.eventSprite.addEventListener(tagType,tagFun,useCapture,priority,useWeakReference);
			//EventUtils.addListener(eventSprite,tagType,tagFun,tagObject,null,useCapture,priority,useWeakReference)
		}
		public static function registLoadContrl(tagLoader:Loader,totalBytes:Number):void
		{
			var loadTime:Number = totalBytes/downLoadSpeed*1000;
			if(loadTime < LOWER_SPEED)
			{
				loadTime = LOWER_SPEED;
			}
			var myTimer:Timer = new Timer(loadTime,1);
			myTimer.addEventListener(TimerEvent.TIMER,timerHandler);
			myTimer.start();
			myDictionary[tagLoader] =  myTimer;
			registLoaderArray.push(tagLoader);
		}
		public static function timerHandler(event:TimerEvent):void
		{
			//加载超时，卸载加载触发 UNLOAD监听事件
			var tempTimer:Timer = event.target as Timer;
			var tempLoader:Loader = getLoaderByTimer(tempTimer);
			tempLoader.unload();
			removeLoadContrl(tempTimer);
		}
		public static function removeLoadContrl(tagTimer:Timer):void
		{
			var tempLoader:Loader = getLoaderByTimer(tagTimer)
			delete myDictionary[tempLoader];
			tagTimer.stop();
			tagTimer.removeEventListener(TimerEvent.TIMER,timerHandler)
			tagTimer = null;
		}
		public static function getLoaderByTimer(tagTimer:Timer):Loader
		{
			for(var i:* in myDictionary)
			{
				if(myDictionary[i] == tagTimer)
				{
					return i;
				}
			}
			return null;
		}
		public static function getLoaderByLoader(tagLoader:Loader):Loader
		{
			for(var i:* in myDictionary)
			{
				if(i == tagLoader)
				{
					return i;
				}
			}
			return null;
		}
		public static function getTimerByLoader(tagLoader:Loader):Timer
		{
			return myDictionary[tagLoader]
		}
	}
}